package com.zhaogang.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhaogang.dao.OrderMapper;
import com.zhaogang.dao.SaleGoodsMapper;
import com.zhaogang.dto.AddSaleSumRequest;
import com.zhaogang.dto.AdminOrderDto;
import com.zhaogang.dto.GoodsInfoResponse;
import com.zhaogang.dto.OrderInfoResponse;
import com.zhaogang.dto.ReceiptInfoRequest;
import com.zhaogang.dto.SaleOrderManageDTO;
import com.zhaogang.entity.Order;
import com.zhaogang.entity.Page;
import com.zhaogang.entity.SaleGoods;
import com.zhaogang.service.OrderService;


@Service
public class OrderServiceImpl implements OrderService {
	
	@Resource
	private OrderMapper orderMapper;
	@Resource
	private SaleGoodsMapper saleGoodsMapper;

	public boolean AddOrder(Order record) {
		
		SaleGoods  saleGoods = saleGoodsMapper.queryBysale_goodsId(record.getSaleGoodsId());
		long saleSum = saleGoods.getSaleSum();
		//System.out.println("saleSum:"+saleSum + " record.getNum():" + record.getNum());
		if(record.getNum() <= saleSum){
			
			//1、减少库存数量
			SaleGoods newOne = new SaleGoods();
				newOne.setId(saleGoods.getId());
				newOne.setSaleSum(saleSum - record.getNum());
			saleGoodsMapper.updateByPrimaryKeySelective(newOne);
			//2、加入订单
			orderMapper.AddOrder(record);
		
			return true;
		}else{
			return false;
		}
	}
	
	public OrderInfoResponse selectOrder(Integer id) {
		OrderInfoResponse oir = new OrderInfoResponse();
		//根据id查询订单相关信息
		oir = orderMapper.selectOrderInfo(id);
		
		return oir;
	}
	
	//@Scheduled(cron = "1/5 * * * * ?")
	public void payOrder(Integer orderId, Date commitDate) {
		System.out.println("aaa");
		
		
	}
	
	//分页查询商户所有订单
	public List<SaleOrderManageDTO> querySaleOrderList(Integer saleAccountId,Page p){
	    return orderMapper.querySaleOrderList(saleAccountId,p.getStartPos(),p.getPageSize());
	}
	
	//条件查询商户订单
			public List<SaleOrderManageDTO> querySaleOrderByCon1(SaleOrderManageDTO saleOrderDto,Page p){
				return orderMapper.querySaleOrderByCon1(saleOrderDto.getSaleAccountId(),
						saleOrderDto.getState(),
						saleOrderDto.getGoodsName(),
						p.getStartPos(),p.getPageSize());
			}
	
	
	
	/**
	 * 分页
	 */
	public Page findPage(Page page) {
		int totalCount = orderMapper.queryCount();
		Integer pageNow;
		pageNow=page.getPageNow();
		Page p =new Page(totalCount, pageNow);
		p.setTotalCount(totalCount);
		return p;
	}
	
	public Page findPage1(Page page,Integer id,String realname,String state) {
        int totalCount = orderMapper.queryCount1( id, realname, state);
        Integer pageNow;
        pageNow=page.getPageNow();
        Page p =new Page(totalCount, pageNow);
        p.setTotalCount(totalCount);
        return p;
    }
	
	
	public Page findSaleOrderPage1(SaleOrderManageDTO saleOrderDto,Page page){
		int totalCount=orderMapper.querySaleOrderCount1(saleOrderDto);  //总记录数
		Integer pageNow;
		pageNow=page.getPageNow();
		Page p=new Page(totalCount,pageNow);
		p.setTotalCount(totalCount);
		return p;
	}
	
	/**
	 * 查询所有商户
	 */
	public List<AdminOrderDto> adminQueryOrder(Page p) {
		// TODO Auto-generated method stub
		return orderMapper.adminQueryOrder(p.getStartPos(), p.getPageSize()) ;
	}

	
	/**
	 * 条件查询
	 */
	public List<AdminOrderDto> orderQueryByCon(Integer id, String realname, 
	        String state,Page p) {
		return orderMapper.orderQueryByCon(id, realname, state,p.getStartPos(), p.getPageSize());
	}
	
	public int updateValid(int orderId) {
		return orderMapper.updateValid(orderId);
		 
	}
	
	public int updateReceiptInfo(ReceiptInfoRequest rir) {
		
		return orderMapper.updateReceiptInfo(rir);
	}
	
	@Transactional
	public boolean delOrder(Integer orderId) {
		OrderInfoResponse oir = new OrderInfoResponse();
		if(!orderId.equals(null)){
			//根据id查询订单相关信息
			
			oir = orderMapper.selectOrderInfo(orderId);
			if(oir == null){
				return false;
			}
			int saleGoodsId = oir.getSaleGoodsId();
			int num = oir.getNum();
			
			//1、增加库存数量
			AddSaleSumRequest addSaleSumRequest = new AddSaleSumRequest();
			addSaleSumRequest.setSaleGoodsId(saleGoodsId);
			addSaleSumRequest.setOrderNum(num);
			int add = saleGoodsMapper.addSaleSum(addSaleSumRequest);
			//2 删除订单
			int row = orderMapper.delOrder(orderId);
			if(add>0 && row>0){
				return true;
			}
			return false;
		}else{
			return false;
		}
		
	}

	public GoodsInfoResponse selectSaleGoodsInfo(int saleGoodsId) {
		return orderMapper.selectSaleGoodsInfo(saleGoodsId);
	}
	
	public List<Order> selectAllNoEndOrder() {
		List<Order> orderList = saleGoodsMapper.selectAllNoEndOrder();
		return orderList;
	}

	public boolean delOrderTimer(int orderId, int num, int saleGoodsId) {
		//1、增加库存数量
		AddSaleSumRequest addSaleSumRequest = new AddSaleSumRequest();
		addSaleSumRequest.setSaleGoodsId(saleGoodsId);
		addSaleSumRequest.setOrderNum(num);
		int add = saleGoodsMapper.addSaleSum(addSaleSumRequest);
		//2 删除订单
		int row = orderMapper.delOrder(orderId);
		if(add>0 && row>0){
			return true;
		}
		
		return false;
	}
}
